﻿using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour
{
    public GameObject enemy;
    public GameObject entry;
    public GameObject exit;
    // Use this for initialization
    void Start()
    {
        this.transform.position = entry.transform.position;
        StartCoroutine(CheckWin());

    }

    // Update is called once per frame
    void Update()
    {

    }
    void On_JoystickMove(MovingJoystick move)
    {
        this.transform.LookAt(this.transform.position + new Vector3(move.joystickAxis.x, 0, move.joystickAxis.y));

    }
    void On_ButtonUp(string buttonName)
    {
        Attack();
    }

    void Attack()
    {
        var attackTrigger = new GameObject("AttackTrigger");
        attackTrigger.transform.position = this.transform.GetChild(0).position;
        attackTrigger.AddComponent<SphereCollider>();
        attackTrigger.GetComponent<SphereCollider>().radius = 0.1f;
        attackTrigger.GetComponent<SphereCollider>().isTrigger = true;
        attackTrigger.tag = "PlayerAttack";
        StartCoroutine(DestroyAttackTrigger(attackTrigger));
    }
    IEnumerator DestroyAttackTrigger(GameObject ob)
    {
        yield return new WaitForSeconds(1);
        Destroy(ob);
    }
    IEnumerator CheckWin()
    {
        yield return new WaitUntil(() => Calculator.Distance(this.transform.position, exit.transform.position) <= 1);
        Time.timeScale = 0;
        Win();
    }
    void Win()
    {
        Debug.Log("YOU WIN!");
    }
}
